package com.lu.pojo.oop.commoninterface.comparator;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * @author luzhenfang
 * @date 2020 05 19
 * @description comparator 的简单使用
 */
public class TestComparable {
    public static void main(String[] args) {
        // comparable 接口 简单使用
        StudentImpl student = new StudentImpl("001", "张三", 90, 20);
        StudentImpl student2 = new StudentImpl("002", "李四", 93, 19);
        int result = 0;
        result = student.compareTo(student2);
        if (result > 0) {
            System.out.println(student.getName() + "比" + student2.getName() + "成绩好");
        } else if (result < 0) {
            System.out.println(student.getName() + "没" + student2.getName() + "成绩好");
        } else {
            System.out.println("两者一样好");
        }
        NewsImpl news = new NewsImpl(1, "新闻1", "...", 1000);
        NewsImpl news2 = new NewsImpl(2, "新闻2", "...", 800);
        result = news.compareTo(news2);
        if (result > 0) {
            System.out.println(news.getTitle() + "比" + news2.getTitle() + "热度好");
        } else if (result < 0) {
            System.out.println(news.getTitle() + "没" + news2.getTitle() + "热度好");
        } else {
            System.out.println("两者一样好");
        }

        // 简单排序
        List<StudentImpl> students = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            students.add(new StudentImpl(String.valueOf(i),("stu"+i),random.nextInt(100),20));
        }
        students.sort(new Comparator<StudentImpl>() {
            @Override
            public int compare(StudentImpl o1, StudentImpl o2) {
               return o1.getScore()>o2.getScore()?1:-1;
            }
        });
        System.out.println(students.toString());
    }
}
